package com.google.code.bankagregator.adapter.bank.alpha.parser.deposit;

import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.entities.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 05.06.13
 * Time: 5:34
 * To change this template use File | Settings | File Templates.
 */
public class AlphaDepTripleAdvantageParser {
    private Bank bank = null;
    private final String DEPOSIT_URL = "http://www.alfabank.com.ua/retail/deposits/tariffs/priority";
    Document doc = null;
    private static final int INDEX_TR_FOR_TERM = 4;
    private static final int INDEX_MIN_AMOUNT = 2;
    private static final int INDEX_RATE = 4;
    private static final int INDEX_RATE_FOR_UAH = 1;
    private static final int INDEX_RATE_FOR_USD = 2;
    private static final int INDEX_RATE_FOR_EUR = 3;

    public AlphaDepTripleAdvantageParser() throws AdapterConnectException {
        bank = new Bank();
        bank.setName("ПАО «Альфа-Банк» Украина");
        bank.setUrl("http://www.alfabank.com.ua/");

        try {
            doc = Jsoup.connect(DEPOSIT_URL).timeout(20000).get();
        } catch (IOException e) {
            throw new AdapterConnectException(e.getMessage());
        }
    }

    /**
     * Get deposit`s params for UAH
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getUAHDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(getMinAmount().substring(29, 33));
        double rate = Double.parseDouble(getRate(INDEX_RATE_FOR_UAH));
        List<Deposit> depositParam = new AlphaDepTripleAdvantageParser().parser(Currency.UAH, minAmount, rate);
        return depositParam;

    }

    /**
     * Get deposit`s params for USD
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getUSDDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(getMinAmount().substring(40, 43));
        double rate = Double.parseDouble(getRate(INDEX_RATE_FOR_USD));
        List<Deposit> depositParam = new AlphaDepTripleAdvantageParser().parser(Currency.USD, minAmount, rate);
        return depositParam;

    }

    /**
     * Get deposit`s params for EUR
     *
     * @return deposit`s params
     * @throws AdapterConnectException
     */
    public List<Deposit> getEURDeposit() throws AdapterConnectException {
        int minAmount = Integer.parseInt(getMinAmount().substring(49, 52));
        double rate = Double.parseDouble(getRate(INDEX_RATE_FOR_EUR));
        List<Deposit> depositParam = new AlphaDepTripleAdvantageParser().parser(Currency.EUR, minAmount, rate);
        return depositParam;

    }

    /**
     * Get some deposit`s params
     *
     * @param currency
     * @param minAmount
     * @param rate
     * @return some deposit`s params
     */
    public List<Deposit> parser(Currency currency, int minAmount, double rate) {
        Deposit deposit = new Deposit();
        List<Deposit> depositParam = new ArrayList<>();

        Element name = doc.select("h1").first();
        Element refill = doc.select("table[width=100%] tbody tr td[width=48%] p[align=justify]").first();
        if (refill.text().substring(19, 33).equals("без пополнения")) {
            deposit.setRefill(false);
        } else {
            deposit.setRefill(true);
        }
        Element elTerm = doc.select("table[class=data] tbody tr").get(INDEX_TR_FOR_TERM).
                select("td").first().select("p").first();
        int term = Integer.parseInt(elTerm.text().replaceAll("[^0-9.]+", "")) * 31;

        deposit.setType(DepositType.Deposit);
        deposit.setTerm(term);
        deposit.setMinAmount(minAmount);
        deposit.setCurrency(currency);
        deposit.setName(name.text());
        deposit.setDepositRate(rate);
        deposit.setBank(bank);
        deposit.setUrl(DEPOSIT_URL);
        deposit.setState(State.ACTIVE);
        depositParam.add(deposit);
        return depositParam;
    }

    /**
     * Get minAmount
     *
     * @return minAmount
     */
    public String getMinAmount() {
        Element elWithAmount = doc.select("table[width=100%] tbody tr td p").get(INDEX_MIN_AMOUNT);
        return elWithAmount.text();
    }

    /**
     * Get rate
     *
     * @param indexRateForCurrency rate`s index for each currency
     * @return rate
     */
    public String getRate(int indexRateForCurrency) {
        Element elWithAmount = doc.select("table[class=data] tbody tr").get(INDEX_RATE).select("td").get(indexRateForCurrency);
        return elWithAmount.text().replace(",", ".").replace("%", "");
    }
}
